﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine;

namespace PloobsEngine.Utils
{
    /// <summary>
    /// Create Procedural textures
    /// </summary>
    public class TextureCreator
    {
        public static Texture2D CreateColorTexture(int size, Color texCor)
        {
            return CreateColorTexture(size, size, texCor);
        }

        public static Texture2D CreateColorTexture(int sizex,int sizey,Color texCor)
        {
            Texture2D t = new Texture2D(EngineStuff.GraphicsDevice, sizex, sizey);
            Color[] cor = new Color[sizex * sizey];
            for (int i = 0; i < sizex; i++)
            {
                for (int j = 0; j < sizex; j++)
                {
                    cor[i + j * sizex] = texCor;
                }
            }

            t.SetData<Color>(cor);
            //t.Save("bla.png", ImageFileFormat.Png);
            return t;
        }

        public static Texture2D CreateCompleteRandomColorTexture(int size)
        {
            return CreateCompleteRandomColorTexture(size, size);
        }

        public static Texture2D CreateCompleteRandomColorTexture(int sizex,int sizey)
        {
            Texture2D t = new Texture2D(EngineStuff.GraphicsDevice, sizex, sizey);
            Color[] cor = new Color[sizex * sizey];
            for (int i = 0; i < sizex; i++)
            {
                for (int j = 0; j < sizey; j++)
                {
                    cor[i + j * sizex] = StaticRandom.RandomColor();
                }
            }

            t.SetData<Color>(cor);
            //t.Save("bla.png", ImageFileFormat.Png);
            return t;
        }

        public static Texture2D CreateBlackAndWhiteRandomTexture(int size)
        {
            return CreateBlackAndWhiteRandomTexture(size, size);
        }


        public static Texture2D CreateBlackAndWhiteRandomTexture(int sizex,int sizey)
        {
            Texture2D t = new Texture2D(EngineStuff.GraphicsDevice, sizex, sizey);
            Color[] cor = new Color[sizex * sizey];
            for (int i = 0; i < sizex; i++)
            {
                for (int j = 0; j < sizey; j++)
                {
                    float staticRandomRandom = StaticRandom.Random();
                    cor[i + j * sizex] = new Color(staticRandomRandom, staticRandomRandom, staticRandomRandom);
                }
            }

            t.SetData<Color>(cor);
            //t.Save("bla.png", ImageFileFormat.Png);
            return t;
        }

        public static Texture2D CreatePerlinNoiseTexture(int size, float frequencia, float amplitude, float persistence, int octave)
        {
            return CreatePerlinNoiseTexture(size, size, frequencia, amplitude, persistence, octave);
        }

        public static Texture2D CreatePerlinNoiseTexture(int sizex, int sizey,float frequencia, float amplitude, float persistence, int octave)
        {
            PerlinNoise pn = new PerlinNoise(sizex, sizey);
            Texture2D t = new Texture2D(EngineStuff.GraphicsDevice, sizex, sizey);
            Color[] cor = new Color[sizex * sizey];
            for (int i = 0; i < sizex; i++)
            {
                for (int j = 0; j < sizey; j++)
                {
                    float value = pn.GetRandomHeight(i, j, 1, frequencia, amplitude, persistence, octave);
                    value =  0.5f * (1 + value);
                    cor[i + j * sizex] = new Color(value,value,value);
                }
            }

            t.SetData<Color>(cor);
            return t;            
        }
    }
}
